package com.zlq.cn.controller.sign.lovers;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.cache.RedisCacheService;
import com.zlq.cn.cache.RedisKey;
import com.zlq.cn.config.FinderConfiguration;
import com.zlq.cn.dto.param.lovers.LoversBindUserLocationParam;
import com.zlq.cn.entity.BindUserLocation;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.un.BindUserLocationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2020-12-29
 */
@Api(tags = "小情侣-绑定地址")
@RestController
@RequestMapping("/lovers/bind/user/location")
public class LoversBindUserLocationController {

    @Resource
    private BindUserLocationService bindUserLocationService;

    @Resource
    private RedisCacheService redisCacheService;

    @Resource
    private FinderConfiguration finderConfiguration;

    @ApiOperation("绑定地址列表")
    @PostMapping("/list")
    public SignApiResult<List<BindUserLocation>> list(@RequestBody LoversBindUserLocationParam param) {
        User user = SignApiBootstrap.requestUser();
        return SignApiResult.resultSuccess(
                bindUserLocationService.lambdaQuery()
                        .eq(BindUserLocation::getBindUserId, param.getBindUserId())
                        .eq(BindUserLocation::getUserId, user.getId())
                        .list()
        );
    }

    @ApiOperation("新增绑定地址")
    @PostMapping("/add")
    public SignApiResult<String> add(@Valid @RequestBody BindUserLocation bind) {
        User user = SignApiBootstrap.requestUser();
        int count = bindUserLocationService.lambdaQuery().eq(BindUserLocation::getBindUserId, bind.getBindUserId()).count();
        if (count < finderConfiguration.getBindUserLocationCount()) {
            bind.setUserId(user.getId())
                    .setCreateTime(LocalDateTime.now())
                    .setUpdateTime(LocalDateTime.now());
            bindUserLocationService.save(bind);
            List<BindUserLocation> bindUserLocations = redisCacheService.get(RedisKey.createAddressRemind(bind.getToUserId()));
            if (bindUserLocations == null) {
                bindUserLocations = bindUserLocationService.lambdaQuery().eq(BindUserLocation::getToUserId, bind.getToUserId()).list();
                if (bindUserLocations == null) {
                    bindUserLocations = new ArrayList<>();
                }
            }
            bindUserLocations.add(bind);
            redisCacheService.set(RedisKey.createAddressRemind(bind.getToUserId()), bindUserLocations);
            return SignApiResult.resultSuccess("添加成功");
        } else {
            return SignApiResult.resultError("地址超过上限");
        }
    }

    @ApiOperation("删除绑定地址")
    @PostMapping("/delete")
    public SignApiResult<String> delete(@RequestBody BindUserLocation bind) {
        User user = SignApiBootstrap.requestUser();
        BindUserLocation bindUserLocation = bindUserLocationService.getById(bind.getId());
        if (bindUserLocation != null) {
            List<BindUserLocation> bindUserLocations = redisCacheService.get(RedisKey.createAddressRemind(bindUserLocation.getToUserId()));
            if (bindUserLocations != null) {
                bindUserLocations.removeIf(bindUserLocation1 -> bindUserLocation1.getId().equals(bind.getId()));
                redisCacheService.set(RedisKey.createAddressRemind(bindUserLocation.getToUserId()), bindUserLocations);
            }
            bindUserLocationService.remove(new LambdaQueryWrapper<BindUserLocation>()
                    .eq(BindUserLocation::getId, bind.getId())
                    .eq(BindUserLocation::getUserId, user.getId()));
        }
        return SignApiResult.resultSuccess("删除成功");
    }
}

